﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtColorMRUContainer : OfficeArtRecord
    {
        private readonly List<MSOCR> rgmsocr = new List<MSOCR>();

        public override RecType RecordType
        {
            get { return RecType.OfficeArtColorMRUContainer; }
        }

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            Contract.Assert(rhrecVer == 0);
            for (int i = 0; i < rhrecInstance; i++)
            {
                var item = new MSOCR();
                item.Read(context.Reader);
                rgmsocr.Add(item);
            }
        }

        protected override byte Version
        {
            get { return 0; }
        }

        protected override int ByteLength
        {
            get { return Enumerable.Sum(this.rgmsocr, item => MSOCR.ByteLength); }
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            Contract.Assert(rgmsocr.Count == rhrecInstance);
            foreach (var item in rgmsocr)
            {
                item.Write(context.Writer);
            }
        }

        protected override void PrepareHeader(ref OfficeArtRecordHeader rh)
        {
            rh.recInstance = (ushort)rgmsocr.Count;
        }
    }
}